#include "EffectFactory.h"
#include "SpriteAnimation.h"


EffectFactory::EffectFactory(void)
{
}


EffectFactory::~EffectFactory(void)
{
}

EffectFactory* EffectFactory::sharedEffectFactory()
{
	if (!_sharedInstance)
	{
		_sharedInstance = EffectFactory::create();
	}
	return _sharedInstance;
}

bool EffectFactory::init()
{
	if (!CCNode::init())
		return false;

	return true;
}

Effect* EffectFactory::createEffect( int type )
{
	Effect* effect = NULL;
	switch (type)
	{
	case EXPLOSIVE_1:
		{
		effect = Effect::create();
		SpriteAnimation* spriteAni1 = SpriteAnimation::createWith("wsparticle_p01.png", "wsparticle_p01.plist", "c_", 1, 8);
		spriteAni1->setOpacity(100);
		spriteAni1->setScale(1.8f);
		spriteAni1->setTimeStep(0.06f);
		SpriteAnimation* spriteAni2 = SpriteAnimation::createWith("wsparticle_p01.png", "wsparticle_p01.plist", "b_", 1, 8);
		spriteAni2->setScale(1.4f);
		spriteAni2->setTimeStep(0.05f);
		effect->addSpriteAnimation(spriteAni1);
		effect->addSpriteAnimation(spriteAni2);
		}
		break;
	case EXPLOSIVE_2:
		{
		effect = Effect::create();
		SpriteAnimation* spriteAni3 = SpriteAnimation::createWith("wsparticle_p01.png", "wsparticle_p01.plist", "a_", 1, 8);
		spriteAni3->setScale(1.5f);
		spriteAni3->setTimeStep(0.03f);
		effect->addSpriteAnimation(spriteAni3);
		}
		break;
	case SHIELD_EFFECT:
		{
			effect = genShieldEffect();
		}
		break;
	}

	return effect;
}

Effect* EffectFactory::genShieldEffect()
{
	Effect* ef = Effect::create();
	_ccBlendFunc f = {GL_SRC_ALPHA, GL_DST_ALPHA};
	ccColor3B color = ccc3(103, 178, 215);

	float time1 = 0.7f;
	float scale1 = 3.5f;
	float scale1default = 0.5f;
	float timeRotate1 = 3.0f;

	CCSprite* sprite4;
	for (int i = 0; i < 7; i++)
	{
		sprite4 = CCSprite::createWithSpriteFrameName("wsparticle_fodder65.png");
		sprite4->setColor(color);
		sprite4->setScale(scale1default);
		sprite4->setBlendFunc(f);
		//CCSpawn::create(CCScaleTo::create(time1, scale1), CCFadeOut::create(time1), NULL);
		sprite4->runAction(CCRepeatForever::create(CCSequence::create(CCSpawn::create(CCScaleTo::create(time1, scale1), CCFadeOut::create(time1), NULL), CCScaleTo::create(0, scale1default), CCRotateBy::create(0, rand() % 360), NULL)));
		ef->addChild(sprite4);
	}
	sprite4 = CCSprite::createWithSpriteFrameName("wsparticle_08.png");
	sprite4->setColor(color);
	sprite4->setOpacity(200);
	sprite4->setScale(scale1default);
	sprite4->setBlendFunc(f);
	sprite4->runAction(CCRepeatForever::create(CCSequence::create(CCSpawn::create(CCScaleTo::create(time1, scale1), CCFadeOut::create(time1), NULL), CCScaleTo::create(0, scale1default), CCRotateBy::create(0, 5), NULL)));
	ef->addChild(sprite4);

	// Vong tron xung quanh
	for (int i = 0; i < 4; i++)
	{
		sprite4 = CCSprite::createWithSpriteFrameName("wsparticle_fodder64.png");
		sprite4->setColor(color);
		sprite4->setBlendFunc(f);
		sprite4->setScale(1);
		sprite4->runAction(CCRepeatForever::create(CCRotateBy::create(rand() %2 + 1, 360)));
		ef->addChild(sprite4);
	}

	sprite4 = CCSprite::createWithSpriteFrameName("wsparticle_super02.png");
	sprite4->setColor(color);
	sprite4->setBlendFunc(f);
	sprite4->setScale(2.5f);
	sprite4->runAction(CCRepeatForever::create(CCRotateBy::create(1, 360)));
	ef->addChild(sprite4);

	sprite4 = CCSprite::createWithSpriteFrameName("wsparticle_fodder64.png");
	sprite4->setColor(color);
	sprite4->setBlendFunc(f);
	sprite4->setScale(1.5f);
	ef->addChild(sprite4);


	return ef;
}

EffectFactory* EffectFactory::_sharedInstance = NULL;

